<template>
  <div class="myContainer">
    <!-- 头部标题 start -->  
    <el-row class="myBorderBottom">
      <el-col :span="7" class="myTitleText myLineHeight_40px">{{title}}</el-col>
      <el-col :span="17" class="myTextAlignRight">
        <!-- <el-button type="primary" class="myBgGreen">新增</el-button> -->
      </el-col>
    </el-row>
    <!-- 头部标题 end -->
    <el-row :gutter="10" class="myPaddingTop_10px">
      <el-col :span="3" class="myLeftMsg">管理员姓名:</el-col>
      <el-col :span="7">
        <el-input 
          placeholder="请选择管理员姓名" 
          suffix-icon="el-icon-check"
          v-model="personNameList"
          @focus="getPerson">
        </el-input>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="myPaddingTop_10px">
      <el-col :span="3" class="myLeftMsg">管理区域:</el-col>
      <el-col :span="7">
        <el-tree
          :data="treeData"
          show-checkbox
          node-key="value"
          ref="region"
          :props="defaultProps"
          @check-change="getAreaInfo"
          :default-expanded-keys="[]"
          :default-checked-keys="[]">
        </el-tree>
      </el-col>
    </el-row>
    <el-row class="myPaddingTop_10px">
      <el-col :span="24">
        <el-button type="primary" class="myBgGreen" v-if="type=='add'" @click="add">新增</el-button>
        <el-button type="primary" class="myBgGreen" v-if="type=='update'" @click="save">保存</el-button>
        <el-button type="primary" class="myBgGreen" @click="back">返回</el-button>
      </el-col>
    </el-row>


    <el-dialog title="选择人员" class="myRoleDialog" :visible.sync="newPersonDialogVisible">
      <el-row :gutter="10">
        <el-col :span="20">
          <el-input placeholder="输入关键词" v-model="kw_person" autofocus></el-input>
        </el-col>
        <el-col :span="4" class="myTextAlignRight">
          <el-button type="primary" class="myBgGreen" @click="onSearchPerson">查询</el-button>
        </el-col>
      </el-row>
      <div class="myPaddingTop_10px">
        <el-tree 
          key="personDlg"
          class="myOV_y_400px"
          ref="personDlg"
          v-loading="personLoading"
          :data="personDlgList"
          :props="personDlgProps"
          node-key="staff_id"
          @check-change="onChange"
          :render-content="renderContent"
          show-checkbox>
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="myBgGreen" @click="newPersonDialogVisible = false">取 消</el-button>
        <el-button @click="handlePersonClick" type="primary" class="myBgGreen">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>     
  
<style scoped>
  .myBorderBottom{
    border-bottom:1px solid #dbdbdb;
    padding-bottom: 5px;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleText{
    font-weight: 500;
    font-size: 16px;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }  
  .myred{
    color: #f00;
    font-size: 12px;
  }
  .myMarginBottom_10px{
    margin-bottom: 10px;
  }
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
  }
  .myOV_y_400px{
    border:1px solid #f2f2f2;
    box-sizing: border-box;
    min-height: 200px;
    max-height: 400px;
    overflow-y: scroll; 
  }
</style>

<script>
  import { getStaffList } from '@/api/staff';
  import { addAdmin , upadteAdmin , getAdminDetail } from "@/api/assets";
  export default {
    data(){
      return{
        title:"资产管理员设置",

        kw_person:"",
        personLoading:false,
        personDlgList:[],
        newPersonDialogVisible:false,
        personDlgProps: {
          children: 'children',
          label: 'user_name'
        },
        personIdList:"",
        personNameList:"",

        type:"",
        id:"",
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        phone:"",
        deptname:"",
        rolename:"",
        areaname:"",
        areaid:""


      }
    },
    components: {

    },
    created(){
      this.type=this.$route.query.type;
      this.id=this.$route.query.id;
      this.treeData=JSON.parse(window.localStorage.getItem("region"));
      if(this.type=="add"){
        document.title="新增资产管理员";
      }else if(this.type=="update"){
        document.title="编辑资产管理员";
        this.ongetAdminDetail();
      }
    },
    watch: {
      
    },
    methods:{
      getPerson(){
        this.newPersonDialogVisible=true;
        this.onGetPersonList();
      },
      onGetPersonList(){//拉取人员
        let vm=this;
        let params = {
          page_num: 1,
          page_size: 10000,
          kw: vm.kw_person
        };
        vm.personLoading=true;
        getStaffList(params).then(response => {
          let res = response.data;
          vm.personDlgList = res.rows;
          vm.personLoading=false;
        }).catch(error => {
          vm.personLoading=false;
        })
      },
      handlePersonClick(){//确认人员
        let vm=this;
        let ids="",names="";
        let person = vm.$refs.personDlg.getCheckedNodes();
        if(person.length>0){
          vm.personIdList=person[0].staff_id;
          vm.personNameList=person[0].user_name;
          vm.phone=person[0].phone;
          vm.deptname=person[0].dept_name;
          vm.rolename=person[0].role_name;
        }else{
          vm.personIdList="";
          vm.personNameList="";
          vm.phone="";
          vm.deptname="";
          vm.rolename="";
        }
        vm.newPersonDialogVisible=false;
      },
      onSearchPerson(){//人员查询
        this.onGetPersonList();
      },
      onChange(a,b,c){
        let vm=this;
        if(b){
          vm.$refs.personDlg.setCheckedKeys([]);
          vm.$refs.personDlg.setCheckedKeys([a.staff_id]);
        }else{
          vm.$refs.personDlg.setCheckedKeys([]);
        }
      },
      renderContent:function(createElement, { node, data, store }) {
        var vm = this;
        return createElement(
          'div', 
          {
            attrs:{
              style:"width:100%",
              title:`联系方式：${node.data.phone}`
            }
          },
          [
            createElement('span', node.label),
            createElement('span', 
              {attrs:{
                style:"float: right; margin-right: 20px"
              }},
              `${node.data.dept_name}`
            )
          ]
        );
      },
      add(){
        let vm=this;
        if(!vm.personIdList || !vm.personNameList || !vm.phone || !vm.deptname || !vm.rolename || !vm.areaname || !vm.areaid){
          vm.$message.info("请将数据填写完整!");
          return;
        }
        let params={
          admin_id:vm.personIdList,
          admin_name:vm.personNameList,
          admin_phone:vm.phone,
          dept_name:vm.deptname,
          role_name:vm.rolename,
          admin_area_name:vm.areaname,
          admin_area_id:vm.areaid
        }
        addAdmin(params).then(response => {
          vm.$message.success("执行成功！");
          vm.back();
        }).catch(error =>{
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      getAreaInfo(){
        let vm=this;
        let areas=vm.$refs.region.getCheckedNodes();
        let areaids="",areanames="";
        for(let i=0; i<areas.length; i++){
          if(areas[i].level==3){
            areaids+=areas[i].value+',';
            areanames+=areas[i].label+',';
          }
        }
        vm.areaname=areanames.substring(0,areanames.length-1);
        vm.areaid=areaids.substring(0,areaids.length-1);
      },
      back(){
        this.$router.go(-1);
      },
      ongetAdminDetail(){
        let vm=this;
        getAdminDetail(vm.id).then(response => {
          let res=response.data;
          vm.personNameList=res.admin_name;
          vm.personIdList=res.admin_id;
          vm.phone=res.admin_phone;
          vm.deptname=res.dept_name;
          vm.rolename=res.role_name;
          vm.areaname=res.admin_area_name;
          vm.areaid=res.admin_area_id;
          vm.$refs.region.setCheckedKeys(res.admin_area_id.split(","));
        }).catch(error => {
          vm.$message.error("拉取失败！"+error.response.data.error_msg);
        })
      },
      save(){ 
        let vm=this;
        if(!vm.personIdList || !vm.personNameList || !vm.phone || !vm.deptname || !vm.rolename || !vm.areaname || !vm.areaid){
          vm.$message.info("请将数据填写完整!");
          return;
        }
        let params={
          admin_id:vm.personIdList,
          admin_name:vm.personNameList,
          admin_phone:vm.phone,
          dept_name:vm.deptname,
          role_name:vm.rolename,
          admin_area_name:vm.areaname,
          admin_area_id:vm.areaid
        }
        upadteAdmin(vm.id,params).then(response => {
          vm.$message.success("执行成功！");
          vm.back();
        }).catch(error =>{
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      }
    }
}
</script>

